public class Main : Object {
	private static string? ui_name = null;
	[CCode (array_length = false, array_null_terminated = true)]
	private static string[]? css_files = null;
	private static string? query = null;
	private static bool verbose = false;
	private static bool texture_cache = false;

	private const GLib.OptionEntry[] options = {
		{"user-interface", 'i', 0, OptionArg.STRING, ref ui_name, "Use 'ui' as user interface", "<ui>"},
		{"css-file", 's', 0, OptionArg.FILENAME_ARRAY, ref css_files, "Use 'ss' as style sheet", "<ss>"},
		{"query", 'q', 0, OptionArg.STRING, ref query, "Albumlist load-query", "<q>"},
		{"verbose", 'v', 0, OptionArg.NONE, ref verbose, "Print debug messages to stdout", null},
		{"use-texture-cache", 't', 0, OptionArg.NONE, ref texture_cache, "Use Texture-cache (needs driver support)", null},
		{ null }
	};

	public static void log_func (string? log_domain, LogLevelFlags log_levels, string message) {
		stdout.printf ("%s\n", message);
	}

	public static int main (string[] args) {
		try {
			var opt_context = new OptionContext ("- Octopus Audio Player");
			opt_context.set_help_enabled (true);
			opt_context.add_main_entries (options, null);
			opt_context.parse (ref args);
		} catch (OptionError e) {
			stdout.printf ("error: %s\n", e.message);
			stdout.printf ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
			return 0;
		}

		if (verbose) {
			GLib.Log.set_handler (null, GLib.LogLevelFlags.LEVEL_MASK, log_func);
		}

		//create global singleton
		Octopus.Global octopusGlobal = Octopus.Global.get ();
		if ((query != null) && (query != "")) {
			octopusGlobal.set_query (query);
		}
		if (ui_name == null) {
			//default (vala) UI
			GLib.debug ("default UI");
			if (css_files == null) {
				string filename = GLib.Path.build_filename (Config.DATAPATH, "octopus", "octopus-mx.css");
				GLib.debug ("css_file: %s", filename);
				octopusGlobal.css_files_add (filename);
				filename = GLib.Path.build_filename (Config.DATAPATH, "octopus", "octopus-gtk.css");
				GLib.debug ("css_file: %s", filename);
				octopusGlobal.css_files_add (filename);
			} else {
				foreach (string s in css_files) {
					GLib.debug ("CSS: %s", s);
					string filename = GLib.Path.build_filename (Config.DATAPATH, ui_name, "style", s);
					GLib.debug ("css_file: %s", filename);
					octopusGlobal.css_files_add (filename);
				}
			}
			octopusGlobal.init_finished.connect (() => {
				Gdk.disable_multidevice ();
				GtkClutter.init (ref args);
				OctopusUi.UiMain window = new OctopusUi.UiMain ();
				window.use_texture_cache = texture_cache;
				window.show_all();
				Gtk.main ();
			});
			octopusGlobal.init ();
		} else {
			//alternative (JavaScript) UI
			GLib.debug ("UI:  %s", ui_name);
			foreach (string s in css_files) {
				GLib.debug ("CSS: %s", s);
				string filename = GLib.Path.build_filename (Config.DATAPATH, ui_name, "style", s);
				GLib.debug ("css_file: %s", filename);
				octopusGlobal.css_files_add (filename);
			}
			//js-context
			octopusGlobal.init_finished.connect (() => {
				octopus_extern_create_js_context (ui_name);
			});
			octopusGlobal.init ();
		}

		return 0;
	}
}
